import Vue from "vue";
import VueRouter from "vue-router";
import HomeView from "../views/home/HomeView.vue";
import store from "@/store";
Vue.use(VueRouter);

import { Toast } from "vant";

const routes = [
  {
    path: "/",
    name: "home",
    component: HomeView,
    meta:{
      cache:true
    }
  },
  {
    path: "/publish",
    name: "publish",
    component: () => import("../views/PublishView.vue"),
    meta:{
      cache:true
    }
  },
  {
    path: "/my",
    name: "my",
    component: () => import("../views/MyView.vue"),
  },
  {
    path: "/reg",
    name: "reg",
    component: () => import("../views/RegView.vue"),
  },
  {
    path: "/login",
    name: "login",
    component: () => import("../views/LoginView.vue"),
  },
  {
    path: "/userinfo",
    name: "userinfo",
    component: () => import("../views/userinfo/UserInfo.vue"),
  },
  {
    path: "/article",
    name: "article",
    component: () => import("../views/article/index.vue"),
  },
  {
    path: "/search",
    name: "search",
    component: () => import("../views/search/search.vue"),
  },
  {
    path: "/searchList",
    name: "searchList",
    component: () => import("../views/search/searchList.vue"),
  },
  {
    path: "/MyPublish",
    name: "MyPublish",
    component: () => import("../views/MyPublish.vue"),
  },
];

const router = new VueRouter({
  routes,
});

//  切换之前 的一个路由钩子

let authPageList = ["/userinfo", "/edit"]; //  需要检测的页面

router.beforeEach((to, from, next) => {
  if (authPageList.includes(to.path)) {
    // 假如想去 一些需要权限才能访问的页面
    if (store.state.user.isLogin) {
      //  假如登陆  直接放行
      next();
    } else {
      //  mei 登陆  让他跳去登陆
      Toast("您还未登录,请先登陆");
      setTimeout((v) => {
        router.push("/login");
      }, 600);
    }
  } else {
    // 假如去的是一些公共的 不敏感 的页面
    next();
  }
});




export default router;
